package com.example;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Map;
import java.util.UUID;

@SpringBootTest
public class BeanUtilTest {

    @Test
    public void test() {
        Person person = new Person();
        person.setAge(14);
        person.setName("测试A11");

        // 将对象转换为 Map
        Map<String, Object> map = BeanUtil.beanToMap(person);
        System.out.println(map);

        // Bean转Bean
        // Bean之间的转换主要是相同属性和属性名的复制，因此方法名为copyProperties，此方法支持Bean和Map之间的字段复制。
        SubPerson subPerson = new SubPerson();
        BeanUtil.copyProperties(person, subPerson);
        String jsonStr = JSONUtil.toJsonStr(subPerson);
        System.out.println(jsonStr);
    }

    @Getter
    @Setter
    public static class Person{
        private String name;
        private int age;
    }

    @Getter
    @Setter
    public static class SubPerson {
        private String name;
        private int age;
        private String password;
    }
}
